//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.yizhilu.os.mobile.front;

import com.yizhilu.os.common.util.WeChatUtil;
import com.yizhilu.os.edu.common.MobileBaseController;
import com.yizhilu.os.edu.entity.user.User;
import com.yizhilu.os.edu.entity.user.UserFeedback;
import com.yizhilu.os.edu.entity.user.UserForm;
import com.yizhilu.os.edu.service.course.CourseService;
import com.yizhilu.os.edu.service.user.UserFeedbackService;
import com.yizhilu.os.edu.service.user.UserService;
import com.yizhilu.os.edu.service.website.WebsiteImagesService;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yizhilu.os.edu.service.kuaijidao.UserKuaijiDaoService;
import com.yizhilu.os.edu.entity.kuaijidao.WeixinBind;

@Controller
public class MobileFrontController extends MobileBaseController {
    private static final Logger logger = LoggerFactory.getLogger(MobileFrontController.class);
    private static String getIndexpage = getViewPath("/front/index");
    private static String getStartpage = getViewPath("/front/start_page");
    private static String msg_success = getViewPath("/common/msg_success");
    private static String feedback = getViewPath("/front/help-fb");
    private static String getBindPage = getViewPath("/user/bind");
    @Autowired
    private CourseService courseService;
    @Autowired
    private WebsiteImagesService websiteImagesService;

    @Autowired
    private UserFeedbackService userFeedbackService;

    @Autowired
    private UserKuaijiDaoService userKuaijiDaoService;

    @Autowired
    private UserService userService;

    public MobileFrontController() {
    }

    @InitBinder({"userFeedBack"})
    public void initBinderQueryCourse(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("userFeedBack.");
    }

    @RequestMapping({"/mobile/start"})
    public String getStartpage(HttpServletRequest request) {
        return getStartpage;
    }

    @RequestMapping({"/mobile/success"})
    public String getMobileSuccess(HttpServletRequest request) {
        request.setAttribute("msg", request.getParameter("msg"));
        return msg_success;
    }

    @RequestMapping({"/mobile/index"})
    public String getIndexpage(HttpServletRequest request, HttpServletResponse response, Model model) {
        return getIndexpage;
    }

    @RequestMapping({"/mobile/bind"})
    public String userWeixinBind(HttpServletRequest request, HttpServletResponse response, Model model) {
        String code =request.getParameter("code");
        String openId =request.getParameter("openId");
        
        if(code!=null){
            openId  = WeChatUtil.getAccessOpenId(code);
        }
       
        List result=userKuaijiDaoService.getUserByOpenId(openId);
        
        if(result==null || result.size()==0){
            System.out.println("++openId++"+openId);
            //保存openId到session
            request.getSession().setAttribute("openId",openId);
            return getBindPage;
        }else{
            Map weixinBind =(HashMap)result.get(0);
            request.getSession().setAttribute("userId",weixinBind.get("userId"));
            System.out.print("do userId ++++++++++++++"+ weixinBind.get("userId"));
            return "redirect:/mobile/dologin";
        }

    }
    
    
    @RequestMapping({"/mobile/zuoti"})
    public String userWeixinBindZuoti(HttpServletRequest request, HttpServletResponse response, Model model) {
       
       String code =request.getParameter("code");
       String openId  = WeChatUtil.getAccessOpenId(code);
     
       
       System.out.print("redirect:http://wxapp.kuaijidao.cn/m/login.php?wxid="+openId);
       return "redirect:http://wxapp.kuaijidao.cn/m/login.php?wxid="+openId;
       
    }


    @RequestMapping({"/mobile/dologin"})
    public String doWeixinlogin(@ModelAttribute("userForm") UserForm userForm, HttpServletRequest request, HttpServletResponse response)
    {
        try{
            List<User> list = null;
            String userId =(String) request.getSession().getAttribute("userId");
            User user = this.userService.getUserById(Long.parseLong(userId));
            String autoThirty = request.getParameter("autoThirty");
            this.userService.setLoginStatus(user, autoThirty, request, response);
            System.out.print("do weixin login ++++++++++++++");
            return "redirect:/mobile/uc/home";

        }catch (Exception e)
        {
            e.printStackTrace();
            System.out.print("do weixin login errot ++++++++++++++");
            return "redirect:/mobile/course/list";

        }

    }


    @RequestMapping({"/mobile/feedback"})
    public String getMobileFeedback(HttpServletRequest request) {
        return feedback;
    }

    @RequestMapping({"/mobile/feedback/add"})
    @ResponseBody
    public Map<String, Object> addMobileFeedback(HttpServletRequest request, UserFeedback userFeedback) {
        Map json = null;

        try {
            String e = request.getParameter("contact");
            if(e != null && !e.equals("")) {
                String regEx = "^1{1}[0-9]{10}$";
                Pattern pat = Pattern.compile(regEx);
                Matcher mat = pat.matcher(e);
                boolean rs = mat.find();
                if(rs) {
                    userFeedback.setMobile(e);
                    userFeedback.setQq("");
                    userFeedback.setEmail("");
                } else {
                    Pattern emailRes = Pattern.compile("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
                    mat = emailRes.matcher(e);
                    boolean emailF = mat.matches();
                    if(emailF) {
                        userFeedback.setEmail(e);
                        userFeedback.setMobile("");
                        userFeedback.setQq("");
                    } else {
                        userFeedback.setQq(e);
                        userFeedback.setMobile("");
                        userFeedback.setEmail("");
                    }
                }
            }

            userFeedback.setName("");
            this.userFeedbackService.addUserFeedback(userFeedback);
            json = this.getJsonMap(true, "true", (Object)null);
        } catch (Exception var11) {
            logger.error("MobileFrontController.addMobileFeedback(", var11);
            json = this.getJsonMap(false, "error", (Object)null);
        }

        return json;
    }
}
